package com.net.protocol.message.body.receiver;

import com.net.protocol.message.Message;
import com.net.protocol.model.RecordFile;
import com.net.protocol.util.BitUtils;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class Search_ACK implements Message.Body.Receiver.ACK {
    private int msgId; //应答ID(对应的终端消息的ID)
    private int seqId; //应答流水号(对应的终端消息的流水号)
    public byte result; //返回结果(0:成功/确认;1:失败)
    public byte fileCount; //文件数量
    public ArrayList<RecordFile> fileLists;

    public int getSeqId() {
        return seqId;
    }

    public int getMsgId() {
        return msgId;
    }

    public byte getResult() {
        return result;
    }

    @Override
    public String toString() {
        return "CommonACK{" + "seqId=" + seqId + ", msgId=" + msgId + ", result=" + result + '}';
    }

    @Override
    public void unpack(byte[] buf, int offset, int length) throws UnsupportedEncodingException {
        msgId = BitUtils.readInt16(buf, offset);
        seqId = BitUtils.readInt16(buf, offset + 2);
        result = buf[offset+4];
        fileCount = buf[offset+5];
        fileLists = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        for (int i = 0; i < fileCount; i++) {
            String fileName = BitUtils.readBCD(buf, offset + 6 + (i * 31), 15);
            int size = BitUtils.readInt32(buf, offset + 21 + (i * 31));
            int startYear = buf[offset+25+(i*31)];
            int startMonth = buf[offset+26+(i*31)];
            int startDay = buf[offset+27+(i*31)];
            int startHour = buf[offset+28+(i*31)];
            int startMin = buf[offset+29+(i*31)];
            int startMill = buf[offset+30+(i*31)];
            int endYear = buf[offset+31+(i*31)];
            int endMonth = buf[offset+32+(i*31)];
            int endDay = buf[offset+33+(i*31)];
            int endHour = buf[offset+34+(i*31)];
            int endMin = buf[offset+35+(i*31)];
            int endMill = buf[offset+36+(i*31)];
            Calendar.getInstance().set(startYear+2000,startMonth,startDay,startHour,startMin,startMill);
            String startTime = sdf.format(Calendar.getInstance().getTime());
            Calendar.getInstance().set(endYear+2000,endMonth,endDay,endHour,endMin,endMill);
            String endTime = sdf.format(Calendar.getInstance().getTime());
            RecordFile file = new RecordFile(msgId,seqId,result,fileName,size,startTime,endTime);
            fileLists.add(file);
        }


    }
}
